﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace Detai.Lib.Helpers
{
	public static class ActionImageHelpers
	{
		public static string ActionImage(this HtmlHelper helper, string linkText, string imageName, string controller, string action, IDictionary<string, object> routeValues)
		{
			return ActionImage(helper, linkText, imageName, controller, action, routeValues, false);
		}
		public static string ActionImage(this HtmlHelper helper, string linkText, string imageName, string controller, string action, IDictionary<string, object> routeValues, bool displayText)
		{
			StringBuilder actionLink = new StringBuilder();
			actionLink.Append("/").Append(controller).Append("/").Append(action);
			if (routeValues != null)
			{
				foreach (var k in routeValues.Keys)
				{
					actionLink.Append("/").Append(routeValues[k].ToString());
				}
			}
			var img = string.Format("<img src=\"/Images/{0}\" alt=\"{1}\" style=\"vertical-align: middle\" />", imageName, linkText);
			if (displayText)
			{
				return string.Format("<a href=\"{0}\" title=\"{1}\">{2} {1}</a>", actionLink.ToString(), linkText, img);
			}
			else
			{
				return string.Format("<a href=\"{0}\" title=\"{1}\">{2}</a>", actionLink.ToString(), linkText, img);
			}
		}
        /// <summary>
        /// Image Action none link
        /// Using JS to action
        /// Created by TungCT
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="linkText"></param>
        /// <param name="imageName"></param>
        /// <param name="controller"></param>
        /// <param name="JsAction">Function in javascript, maybe within paramaters. Ext: Caculating(a,b)- a,b is paramaters </param>
        /// <param name="displayText"></param>
        /// <returns></returns>
        public static string ActionImageJS(this HtmlHelper helper, string linkText, string imageName, string JsAction, bool displayText)
        {
            var img = string.Format("<img src=\"/Images/{0}\" alt=\"{1}\" style=\"vertical-align: middle\" />", imageName, linkText);
            if (displayText)
            {
                return string.Format("<a href=\"JavaScript:{0}\" title=\"{1}\">{2} {1}</a>", JsAction, linkText, img);
            }
            else
            {
                return string.Format("<a href=\"JavaScript:{0}\" title=\"{1}\">{2}</a>", JsAction, linkText, img);
            }
        }
        /// <summary>
        /// Link action with Javascript and text link default = false
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="linkText"></param>
        /// <param name="imageName"></param>
        /// <param name="JsAction"></param>
        /// <returns></returns>
        public static string ActionImageJS(this HtmlHelper helper, string linkText, string imageName, string JsAction)
        {
            return ActionImageJS(helper, linkText, imageName, JsAction, false);
        }


		public static string Toolbar(this HtmlHelper helper, string linkText, string targetLink, string task)
		{
			StringBuilder toolbar = new StringBuilder();
			toolbar.Append(string.Format("<td id=\"toolbar-{0}\" class=\"button\">", task));
			toolbar.Append(string.Format("<a class=\"toolbar\" href=\"{0}\">", targetLink));
			toolbar.Append(string.Format("<span title=\"{0}\" class=\"icon-32-{1}\"></span>",linkText, task));
			toolbar.Append(linkText);
			toolbar.Append("</a>");
			toolbar.Append("</td>");
			return toolbar.ToString();
		}
        public static string Toolbar(this HtmlHelper helper, string linkText, string targetLink, string task, string target)
        {
            StringBuilder toolbar = new StringBuilder();
            toolbar.Append(string.Format("<td id=\"toolbar-{0}\" class=\"button\">", task));
            toolbar.Append(string.Format("<a class=\"toolbar\" href=\"{0}\" target='{1}'>", targetLink, target));
            toolbar.Append(string.Format("<span title=\"{0}\" class=\"icon-32-{1}\"></span>",linkText, task));
            toolbar.Append(linkText);
            toolbar.Append("</a>");
            toolbar.Append("</td>");
            return toolbar.ToString();
        }
	}
}
